package com.jzo2o.customer.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.CurrentUserInfo;
import com.jzo2o.customer.mapper.ICertificationMapper;
import com.jzo2o.customer.mapper.WorkerCertificationMapper;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationRecording;
import com.jzo2o.customer.model.dto.WorkerCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.WorkerCertificationAuditAddReqDTO;
import com.jzo2o.customer.model.dto.response.RejectReasonResDTO;
import com.jzo2o.customer.service.ICertificationService;
import com.jzo2o.mvc.utils.UserContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;


@Service
public class ICertificationServiceImpl extends ServiceImpl<ICertificationMapper, WorkerCertificationRecording> implements ICertificationService {


    @Resource
    private ICertificationMapper iCertificationMapper;

    @Resource
    private WorkerCertificationMapper worker;



    /**
     * 服务端提交认证申请
     * @param auditReqDTO
     */
    @Override
    public void addCertificationAudit(WorkerCertificationAuditAddReqDTO auditReqDTO) {
        Long id = UserContext.currentUserId();
        if(!ObjectUtil.isNotEmpty(auditReqDTO)) {
            throw new ForbiddenOperationException("信息为空无法提交哦亲!!!");
        }
        WorkerCertificationRecording recording
                = BeanUtil.copyProperties(auditReqDTO, WorkerCertificationRecording.class);
        recording.setAuditStatus(0);
        recording.setCertificationStatus(1);
        recording.setServeProviderId(id);
        iCertificationMapper.
                insert(recording);

        WorkerCertification workerCertification1 = worker.selectById(id);
        WorkerCertification workerCertification = BeanUtil.copyProperties(recording, WorkerCertification.class);
        if(ObjectUtil.isEmpty(workerCertification1)){
            workerCertification.setCertificationStatus(1);
            workerCertification.setId(id);
            worker.insert(workerCertification);
        }else {
            workerCertification.setCertificationStatus(1);
            workerCertification.setId(id);
            worker.updateById(workerCertification);
        }
    }



    @Override
    public RejectReasonResDTO getRejectReason() {
        Long id = UserContext.currentUserId();
        WorkerCertification temp  = iCertificationMapper.selectLastRejectReason(id);
        RejectReasonResDTO rejectReasonResDTO = new RejectReasonResDTO();
        rejectReasonResDTO.setRejectReason(temp.getRejectReason());
        return rejectReasonResDTO;
    }
}
